// TradeRecordServiceImpl.java (服务实现类)
package com.ywb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywb.mapper.TradeRecordMapper;
import com.ywb.pojo.PageResult;
import com.ywb.pojo.TradeRecord;
import com.ywb.pojo.TradeRecordQueryParam;
import com.ywb.service.TradeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class TradeRecordServiceImpl implements TradeRecordService {

    @Autowired
    private TradeRecordMapper tradeRecordMapper;

    @Override
    public List<TradeRecord> findAll() {
        return tradeRecordMapper.findAll();
    }

    @Override
    public void deleteById(Long id) {
        tradeRecordMapper.deleteById(id);
    }

    @Override
    public void add(TradeRecord tradeRecord) {
        tradeRecord.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        tradeRecord.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
        log.info("新增交易记录{}", tradeRecord);
        tradeRecordMapper.insert(tradeRecord);
    }

    @Override
    public TradeRecord getById(Long id) {
        return tradeRecordMapper.getById(id);
    }

    @Override
    public void update(TradeRecord tradeRecord) {

        tradeRecord.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
        log.info("修改交易记录{}", tradeRecord);
        tradeRecordMapper.update(tradeRecord);
    }

    @Override
    public PageResult<TradeRecord> page(TradeRecordQueryParam tradeRecordQueryParam) {
        PageHelper.startPage(tradeRecordQueryParam.getPage(), tradeRecordQueryParam.getPageSize());
        List<TradeRecord> tradeRecordlist = tradeRecordMapper.list(tradeRecordQueryParam);
        Page<TradeRecord> p= (Page<TradeRecord>) tradeRecordlist;
        PageResult<TradeRecord> pageResult = new PageResult<>(p.getTotal(),p.getResult());
        return pageResult;
    }

    @Override
    public void delete(List<Integer> ids) {
        tradeRecordMapper.deleteByIds(ids);
    }
}